﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_CommerCheckBuyGoodsContract : DockContent
    {
        private DataSet m_dsPaidan = new DataSet();
        private int m_RowIndex = 0;
        private DGBY.BLL.LogRecTab m_bllLogRecTab = new DGBY.BLL.LogRecTab();

        private DGBY.BLL.SJCKHTTab m_bllSJCKHTTab = new DGBY.BLL.SJCKHTTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.NumDanWeiTab m_bllNumDanWeiTab = new DGBY.BLL.NumDanWeiTab();
        private DGBY.BLL.SJPackGoodsMXTab m_bllSJPackGoodsMXTab = new DGBY.BLL.SJPackGoodsMXTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.SJGouHHTTab m_bllSJGouHHTTab = new DGBY.BLL.SJGouHHTTab();
        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.FacAddrTab m_bllFacAddrTab = new DGBY.BLL.FacAddrTab();
        private DGBY.BLL.FacConPeoAndTelTab m_bllFacConPeoAndTelTab = new DGBY.BLL.FacConPeoAndTelTab();
        
        private int m_nSelectSJPackGoodsMXTabID = 0;
        private int m_nSelectSJGouHHTTabID = 0;

        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        //ExcelBase eB = new ExcelBase();

        public Frm_CommerCheckBuyGoodsContract()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndID("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }
            DataSet ds = m_bllT_ACL_User.GetList("Name != 'System1'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxRequierSideDele.DataSource = ds.Tables[0];
                this.comboBoxRequierSideDele.DisplayMember = "用户名称";
                this.comboBoxRequierSideDele.ValueMember = "UserPasswordTabID";
                this.comboBoxRequierSideDele.SelectedIndex = 0;
            }
        }
        private void SyncContractNo()
        {
            try
            {

                // string str = this.comboBoxContractNo.SelectedValue.ToString();

                int nID = Frm_Main.m_nContractSelectValue;

                DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    //int nCompId = Convert.ToInt32(ds.Tables[0].Rows[0]["BYCompanyTabID"].ToString(), 10);
                    //DataSet ds1 = m_bllBYCompanyTab.GetList("BYCompanyTabID = '" + nCompId + "'");
                    //if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
                    //{
                    //    this.textBoxCompany.Text = ds1.Tables[0].Rows[0]["经营单位名称"].ToString();
                    //}
                    string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                    ShowBuyGoodsSheetByContractCode(strContractCode);
                }



                //this.textBoxMark.Text = "";
                //this.textBoxNote.Text = "";


            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }
        private void InitFactoryCombox(string strFacName)
        {
            DataSet ds = m_bllFactoryDataTab.GetList("FactoryName = '" + strFacName + "'");
            int nFacId = 0;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nFacId = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
            }
            ds = m_bllFacAddrTab.GetList("FactoryDataTabID = '" + nFacId + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxProviderAddr.DataSource = ds.Tables[0];
                this.comboBoxProviderAddr.DisplayMember = "FacAddr";
                this.comboBoxProviderAddr.ValueMember = "FacAddrTabID";
                this.comboBoxProviderAddr.SelectedIndex = 0;
            }
            ds = m_bllFacConPeoAndTelTab.GetList("FactoryDataTabID = '" + nFacId + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxProviderDele.DataSource = ds.Tables[0];
                this.comboBoxProviderDele.DisplayMember = "FacConPeoName";
                this.comboBoxProviderDele.ValueMember = "FacConPeoAndTelTabID";
                this.comboBoxProviderDele.SelectedIndex = 0;
            }
        }

        private void Frm_CommerCheckBuyGoodsContract_Load(object sender, EventArgs e)
        {
            InitCombox();

        }


        private void ShowAllRecordInDataGridView(string strWhere)
        {

            DataSet ds = m_bllSJGouHHTTab.ShowOneBuyGoodsRecEx(strWhere);


            ultraGrid1.DataSource = null;

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }
        private void ShowAllMXRecordInDataGridView(string strContractCode)
        {
            this.textBox1ContractNoGoodsDetail.Text = strContractCode;
            ultraGrid2.DataSource = null;
            DataSet ds = m_bllSJGouHHTTab.ShowAllGouHHTMX("ContractCode = '" + strContractCode + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ds.Tables[0].Columns["打折单价"].ColumnName = "报关单价";
                ds.Tables[0].Columns["打折金额"].ColumnName = "报关金额";
                //Decimal nNumTotal = 0;

                //Decimal dtJinE = 0;
                //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //{

                //    nNumTotal += Convert.ToDecimal(ds.Tables[0].Rows[i]["数量"].ToString());
                //    dtJinE += Convert.ToDecimal(ds.Tables[0].Rows[i]["金额"].ToString());
                //}

                //DataRow dr = ds.Tables[0].NewRow();

                //dr["数量"] = nNumTotal;

                //dr["金额"] = dtJinE;
                //dr["货物名称"] = "合计";
                //ds.Tables[0].Rows.Add(dr.ItemArray);
                ultraGrid2.DataSource = ds.Tables[0];
                ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                
                if (Frm_Main.m_bCompleteVer == false)
                {
                   
                    ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Hidden = true;
                    ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Hidden = true;
                }
            }
            //SetSumFields(strContractCode);
        }
        private void ShowBuyGoodsSheetByContractCode(string strContractCode)
        {
            DataSet ds = m_bllSJPackingTab.ShowOneSJPackRec("PaidanRegisterTab.ContractCode = '" + strContractCode + "'");
            int nPackingId = 0;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nPackingId = Convert.ToInt32(ds.Tables[0].Rows[0]["SJPackingTabID"].ToString(), 10);
            }
           // ds = m_bllSJCKHTTab.ShowOneOutContractRec("ContractCode = '" + strContractCode + "'", nPackingId);
            ds = m_bllSJGouHHTTab.ShowOneBuyGoodsRec("ContractCode = '" + strContractCode + "'", nPackingId);

            ultraGrid2.DataSource = null;

            ultraGrid1.DataSource = null;

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                m_nSelectSJGouHHTTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["SJGouHHTTabID"].ToString());
                //this.textBoxSheetNo.Text = ds.Tables[0].Rows[0]["单据号"].ToString();
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                //this.textBoxExchangeRate.Text = ds.Tables[0].Rows[0]["汇率"].ToString();
                //m_nSelectSJGouHHTTabID = Convert.ToInt32(strSJGouHHTTabID, 10);
                cellSJGouHHTTabClick();
                ShowAllMXRecordInDataGridView(strContractCode);
            }
            else
            {
                ultraGrid2.DataSource = null;
                m_nSelectSJGouHHTTabID = 0;
            }
            
        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();
                int nID = Convert.ToInt32(str, 10);

                DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                DataSet ds2 = m_bllSJGouHHTTab.GetJYCompNameEtc("ContractCode = '" + strContractCode + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    this.textBoxRequierSideName.Text = ds2.Tables[0].Rows[0]["需方名称"].ToString();
                    this.textBoxRequierSideAddr.Text = ds2.Tables[0].Rows[0]["需方地址"].ToString();
                    this.textBoxRequierSideTel.Text = ds2.Tables[0].Rows[0]["需方电话"].ToString();
                }

                ds2 = m_bllSJGouHHTTab.GetFacNameEtc("ContractCode = '" + strContractCode + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    this.textBoxProviderName.Text = ds2.Tables[0].Rows[0]["工厂名称"].ToString();
                    this.textBoxProviderTel.Text = ds2.Tables[0].Rows[0]["工厂电话"].ToString();
                }

                ds2 = m_bllSJGouHHTTab.GetQDDateEtc("ContractCode = '" + strContractCode + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    this.textBoxSignDate.Text = ds2.Tables[0].Rows[0]["签订日期"].ToString();
                    this.textBoxSignPlace.Text = ds2.Tables[0].Rows[0]["签订地点"].ToString();
                    this.textBoxWrapper.Text = ds2.Tables[0].Rows[0]["包装种类"].ToString();
                    this.textBoxCommitGoodsTime.Text = ds2.Tables[0].Rows[0]["交货日期"].ToString();
                }
                this.textBoxExchangeRate.Text = "8.0000";
                //this.textBoxContractNoDetail.Text = strContractCode;
                InitFactoryCombox(this.textBoxProviderName.Text);
                ShowBuyGoodsSheetByContractCode(strContractCode);
                //ds2 = m_bllSJGouHHTTab.ShowZNumZJinE("ContractCode = '" + strContractCode + "'");
                //if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                //{
                //    this.textBoxTotalNum.Text = ds2.Tables[0].Rows[0]["ZNumber"].ToString();
                //    this.textBoxTotalMoney.Text = ds2.Tables[0].Rows[0]["ZGouHHTJinE"].ToString();
                //}
            }
            catch { }
        }

        private Int64 MakeSJPackDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllSJGouHHTTab.GetSJGouHHTTabByDate("SJGouHHTDJDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }



        private void cellSJGouHHTTabClick()
        {
            this.ultraGrid1.Dock = DockStyle.None;
            this.groupBoxGoodsDetail.Visible = true;
            DGBY.Model.SJGouHHTTab modelSJGouHHTTab = m_bllSJGouHHTTab.GetModel(m_nSelectSJGouHHTTabID);
            if (modelSJGouHHTTab != null)
            {
                this.comboBoxRequierSideDele.Text = modelSJGouHHTTab.JYCompDaiBiao;


                this.textBoxWayPlaceCost.Text = modelSJGouHHTTab.JHFSPosYunCost;
                this.textBoxDebtComputeWay.Text = modelSJGouHHTTab.HKJSMethod;
                this.textBoxProductQuality.Text = modelSJGouHHTTab.CPZhiLiang;
                this.textBoxDestroyProtocolRespo.Text = modelSJGouHHTTab.WYZeRen;
                this.textBoxSolveContractWay.Text = modelSJGouHHTTab.JFJieJue;
                this.textBoxOther.Text = modelSJGouHHTTab.QTThing;
                this.textBoxExchangeRate.Text = modelSJGouHHTTab.HuiLv.ToString();
                this.textBoxHote.Text = modelSJGouHHTTab.Note;
                this.textBoxSheetNo.Text = modelSJGouHHTTab.SJGouHHTDJNo.ToString();


                if (modelSJGouHHTTab.PaidanRegisterTabID != null)
                    this.comboBoxContractNo.SelectedValue = modelSJGouHHTTab.PaidanRegisterTabID;

            }
        }



      

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    DataSet ds = m_bllSJCKHTTab.ShowOneOutContractRec("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {

            //    }
            //    else
            //    {
            //        MessageBox.Show("该合同编号没有对应的商检_出口合同!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }

            //    ds = m_bllSJGouHHTTab.ShowOneBuyGoodsRec("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {
            //        MessageBox.Show("该合同编号已有对应的商检_购货合同了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }

            //    if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxExchangeRate))
            //        return;
            //    DGBY.Model.SJGouHHTTab modelSJGouHHTTab = new DGBY.Model.SJGouHHTTab();

            //    modelSJGouHHTTab.JYCompDaiBiao = this.comboBoxRequierSideDele.Text;
            //    modelSJGouHHTTab.FacDaiBiao = this.comboBoxProviderDele.Text;

            //    modelSJGouHHTTab.JHFSPosYunCost = this.textBoxWayPlaceCost.Text;
            //    modelSJGouHHTTab.HKJSMethod = this.textBoxDebtComputeWay.Text;
            //    modelSJGouHHTTab.CPZhiLiang = this.textBoxProductQuality.Text;
            //    modelSJGouHHTTab.WYZeRen = this.textBoxDestroyProtocolRespo.Text;
            //    modelSJGouHHTTab.JFJieJue = this.textBoxSolveContractWay.Text;
            //    modelSJGouHHTTab.QTThing = this.textBoxOther.Text;
            //    modelSJGouHHTTab.HuiLv = Convert.ToDecimal(this.textBoxExchangeRate.Text);
            //    modelSJGouHHTTab.Note = this.textBoxHote.Text;


            //    modelSJGouHHTTab.SJGouHHTDJNo = MakeSJPackDJNo();
            //    modelSJGouHHTTab.SJGouHHTDJDate = System.DateTime.Now.Date;

            //    if (this.comboBoxContractNo.SelectedValue != null)
            //        modelSJGouHHTTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);

            //    //ds = m_bllFactoryDataTab.GetList("FactoryName = '" + this.textBoxProviderName.Text + "'");
            //    //int nFacId = 0;
            //    //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    //{
            //    //    modelSJGouHHTTab.FacAddrTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
            //    //}

            //    if (this.comboBoxProviderAddr.SelectedValue != null)
            //        modelSJGouHHTTab.FacAddrTabID = Convert.ToInt32(this.comboBoxProviderAddr.SelectedValue.ToString(), 10);


            //    m_bllSJGouHHTTab.Add(modelSJGouHHTTab);
            //    ShowAllRecordInDataGridView("PaidanRegisterTab.PaidanRegisterTabID = '" + modelSJGouHHTTab.PaidanRegisterTabID + "'");
            //    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
         
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxExchangeRate))
                    return;
                DGBY.Model.SJGouHHTTab modelSJGouHHTTabOld = m_bllSJGouHHTTab.GetModel(m_nSelectSJGouHHTTabID);
                if (modelSJGouHHTTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //DGBY.Model.SJGouHHTTab modelSJGouHHTTab = new DGBY.Model.SJGouHHTTab();
                //modelSJGouHHTTab.SJGouHHTTabID = m_nSelectSJGouHHTTabID;
               // modelSJGouHHTTabOld.SJGouHHTDJNo = modelSJGouHHTTabOld.SJGouHHTDJNo;

                modelSJGouHHTTabOld.JYCompDaiBiao = this.comboBoxRequierSideDele.Text;
                modelSJGouHHTTabOld.FacDaiBiao = this.comboBoxProviderDele.Text;

                modelSJGouHHTTabOld.JHFSPosYunCost = this.textBoxWayPlaceCost.Text;
                modelSJGouHHTTabOld.HKJSMethod = this.textBoxDebtComputeWay.Text;
                modelSJGouHHTTabOld.CPZhiLiang = this.textBoxProductQuality.Text;
                modelSJGouHHTTabOld.WYZeRen = this.textBoxDestroyProtocolRespo.Text;
                modelSJGouHHTTabOld.JFJieJue = this.textBoxSolveContractWay.Text;
                modelSJGouHHTTabOld.QTThing = this.textBoxOther.Text;
                modelSJGouHHTTabOld.HuiLv = Convert.ToDecimal(this.textBoxExchangeRate.Text);
                modelSJGouHHTTabOld.Note = this.textBoxHote.Text;


                //modelSJGouHHTTabOld.SJGouHHTDJNo = MakeSJPackDJNo();
                //modelSJGouHHTTabOld.SJGouHHTDJDate = System.DateTime.Now.Date;
                modelSJGouHHTTabOld.FacAddrTabID = modelSJGouHHTTabOld.FacAddrTabID;

             //   if (this.comboBoxContractNo.SelectedValue != null)
                //    modelSJGouHHTTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);
                //modelSJGouHHTTabOld.PaidanRegisterTabID = modelSJGouHHTTabOld.PaidanRegisterTabID;

                m_bllSJGouHHTTab.Update(modelSJGouHHTTabOld);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxContractNo.Text);
                ShowBuyGoodsSheetByContractCode(this.comboBoxContractNo.Text);
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            DGBY.Model.SJGouHHTTab modelSJGouHHTTabOld = m_bllSJGouHHTTab.GetModel(m_nSelectSJGouHHTTabID);
            if (modelSJGouHHTTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DGBY.Model.PaidanRegisterTab modelPaidanRegisterTabOld = m_bllPaidanRegisterTab.GetModel(modelSJGouHHTTabOld.PaidanRegisterTabID);


            if (Frm_Main.m_nLoginUserId != modelPaidanRegisterTabOld.UserPasswordTabID)
            {
                MessageBox.Show("该合同号不是你派的单，不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllSJGouHHTTab.Delete(m_nSelectSJGouHHTTabID);

                DGBY.Model.LogRecTab modelLogRecTab = new DGBY.Model.LogRecTab();
                modelLogRecTab.LogDate = System.DateTime.Now.Date;
                modelLogRecTab.LogContent = Frm_Main.m_strLoginUser + "删除了合同号"
                    + modelPaidanRegisterTabOld.ContractCode + "的购货合同草稿";
                m_bllLogRecTab.Add(modelLogRecTab);

                Frm_Main.Log2DB(this.Text, "删除", modelPaidanRegisterTabOld.ContractCode);
                ShowAllRecordInDataGridView("PaidanRegisterTab.PaidanRegisterTabID = '" + m_nSelectSJGouHHTTabID + "'");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxWayPlaceCost.Text = "";
                this.textBoxOther.Text = "";
                this.textBoxHote.Text = "";

                return;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //if (m_nPaidanRegisterTabID != 0)
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                //else
                // this.comboBoxContractNo.SelectedIndex = Frm_Main.m_nContractSelectIndex;
            }
            this.ultraGrid1.Dock = DockStyle.Fill;
            this.groupBoxGoodsDetail.Visible = false;
            ShowAllRecordInDataGridView("");
        }

        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool IsNum(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"^[1-9]+\d*$");
            return bRet;
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        public bool IsDecimal(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text) && !IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNum(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

       

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllSJGouHHTTab.ShowAllGouHHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            //if (Frm_Main.m_bCompleteVer == true)
            //{
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                //dt.Columns["打折单价"].ColumnName = "报关单价";
                //dt.Columns["打折金额"].ColumnName = "报关金额";
            //}
            //else
            //{
            //    dt.Columns.Remove("单价");
            //    dt.Columns.Remove("金额");
            //}
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            //DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            //DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条购货合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "商检_购货合同草稿.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckBuyGoodsConstract_DataTableToExcel(dt, dt1, 20, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllSJGouHHTTab.ShowAllGouHHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            //if (Frm_Main.m_bCompleteVer == true)
            //{
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                //dt.Columns["打折单价"].ColumnName = "报关单价";
                //dt.Columns["打折金额"].ColumnName = "报关金额";
            //}
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条购货合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检_购货合同草稿.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckBuyGoodsConstract_DataTableToExcelAndPrintPreview(dt, dt1, 20, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            DataSet ds = m_bllSJGouHHTTab.ShowAllGouHHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            //if (Frm_Main.m_bCompleteVer == true)
            //{
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                //dt.Columns["打折单价"].ColumnName = "报关单价";
                //dt.Columns["打折金额"].ColumnName = "报关金额";
            //}
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条购货合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检_购货合同草稿.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckBuyGoodsConstract_DataTableToExcelAndPrint(dt, dt1, 20, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Frm_CommerCheckBuyGoodsContract_FormClosing(object sender, FormClosingEventArgs e)
        {
            //eB.Close();
        }

        private void buttonDetailShow_Click(object sender, EventArgs e)
        {
            //int nSelectSJPackingTabID = 0;
            //if (dataGridViewBuyGoods.Rows.Count > 0)
            //{
            //    string strSJPackingTabID = this.dataGridViewBuyGoods.Rows[this.dataGridViewBuyGoods.CurrentCell.RowIndex].Cells[0].Value.ToString();
            //    nSelectSJPackingTabID = Convert.ToInt32(strSJPackingTabID, 10);
            //}
            //Frm_CommerCheckBuyGoodsContractDetail frm_CommerCheckBuyGoodsContractDetail = new Frm_CommerCheckBuyGoodsContractDetail(nSelectSJPackingTabID, this.comboBoxContractNo.Text);
            //frm_CommerCheckBuyGoodsContractDetail.StartPosition = FormStartPosition.CenterScreen;
            //frm_CommerCheckBuyGoodsContractDetail.ShowDialog();
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

            InitColumnWidth1();
        }

        private void InitColumnWidth1()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["单据号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["需方名称"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["需方地址"].Width = 200;
            ultraGrid1.DisplayLayout.Bands[0].Columns["需方电话"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["需方代表"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["供方名称"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["供方地址"].Width = 200;
            ultraGrid1.DisplayLayout.Bands[0].Columns["供方电话"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["供方代表"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["签订日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["签订地点"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["包装"].Width = 30;
            ultraGrid1.DisplayLayout.Bands[0].Columns["交货时间"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["交货方式地点运杂费负担"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["贷款结算方式"].Width = 140;
            ultraGrid1.DisplayLayout.Bands[0].Columns["产品质量"].Width = 140;
            ultraGrid1.DisplayLayout.Bands[0].Columns["违约责任"].Width = 140;
            ultraGrid1.DisplayLayout.Bands[0].Columns["解决合同纠纷方式"].Width = 140;
            ultraGrid1.DisplayLayout.Bands[0].Columns["其它约定事项"].Width = 140;
            ultraGrid1.DisplayLayout.Bands[0].Columns["汇率"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;      
            string strSJGouHHTTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectSJGouHHTTabID = Convert.ToInt32(strSJGouHHTTabID, 10);
            cellSJGouHHTTabClick();
        }

        private void ultraGrid2_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

            InitColumnWidth2();

            List<string> lstTemp = new List<string>();
            lstTemp.Add("数量");
            lstTemp.Add("金额");

            //if (Frm_Main.m_bCompleteVer == true)
            {
                lstTemp.Add("报关金额");
            }

           
            ClassUltraGridCommonSet.SetSumColumn(e, lstTemp);
        }
        //private void cellSJPackGoodsMXTabClick()
        //{
        //    DGBY.Model.SJPackGoodsMXTab modelSJPackGoodsMXTab = m_bllSJPackGoodsMXTab.GetModel(m_nSelectSJPackGoodsMXTabID);
        //    if (modelSJPackGoodsMXTab != null)
        //    {
        //        this.textBoxGoodsName.Text = modelSJPackGoodsMXTab.GoodsMXName;
        //        this.textBoxMerial.Text = modelSJPackGoodsMXTab.GGXHZDEtc;
        //        this.textBoxNum.Text = modelSJPackGoodsMXTab.Number.ToString();

        //        this.textBoxSingleCost.Text = modelSJPackGoodsMXTab.CKHTDCost.ToString();
        //        this.textBoxFOB.Text = modelSJPackGoodsMXTab.CKHTJinE.ToString();


        //        DataSet ds = m_bllBiZhiTab.GetList("BiZhiTabID = '" + modelSJPackGoodsMXTab.BiZhiTabID + "'");
        //        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //        {
        //            this.textBoxCoinUnit.Text = ds.Tables[0].Rows[0]["币制名称"].ToString();
        //        }

        //        ds = m_bllNumDanWeiTab.GetList("NumDanWeiTabID = '" + modelSJPackGoodsMXTab.NumDanWeiTabID + "'");
        //        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //        {
        //            this.textBoxUnit.Text = ds.Tables[0].Rows[0]["单位名称"].ToString();
        //        }
        //    }

        //}
        private void ultraGrid2_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid2.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strTemp = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[1].Text;
            if (strTemp == "合计")
                return;
            m_RowIndex = this.ultraGrid2.ActiveCell.Row.Index;
            ShowGoodsDetail(m_RowIndex);

        }

        private void InitColumnWidth2()
        {
            ultraGrid2.DisplayLayout.Bands[0].Columns["货物名称"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["质地"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["规格型号"].Width = 100;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单位名称"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["数量"].Width = 70;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["报关单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["报关金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["币制"].Width = 40;
        }


        private void ShowGoodsDetail(int RowIndex)
        {
            int i = 1;
            this.textBoxGoodsName.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxMerial.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxGuiGe.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxUnit.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxNum.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxSingleCost.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxFOB.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
            this.textBoxCoinUnit.Text = this.ultraGrid2.Rows[RowIndex].Cells[i+2].Text;
        }
        private void Frm_CommerCheckBuyGoodsContract_Activated(object sender, EventArgs e)
        {
            InitContractNoCombox();
            SyncContractNo();
        }
        private void InitContractNoCombox()
        {
            //DataSet ds = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxContractNo.DataSource = ds.Tables[0];
            //    this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
            //    this.comboBoxContractNo.DisplayMember = "合同编号";
            //    this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            //}

            try
            {
                if (Frm_Main.m_nContractSelectValue == 127)
                    return;
                DataRow[] drs = m_dsPaidan.Tables[0].Select("PaidanRegisterTabID = '" + Frm_Main.m_nContractSelectValue.ToString() + "'");
                if (drs.Length > 0)
                    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                else
                {
                    m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
                    if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
                    {
                        this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                        this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                        this.comboBoxContractNo.DisplayMember = "合同编号";
                        this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                        this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                    }

                    this.comboBoxRequierSideDele.SelectedIndex = 0;

                   
                }
            }
            catch (System.Exception ex)
            {
                //  MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private void Frm_CommerCheckBuyGoodsContract_Deactivate(object sender, EventArgs e)
        {
            try
            {
                Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;

            }
            catch (System.Exception ex)
            {
                //  MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
    }
}